export default class GuessNumber{
    constructor(gameInfo,room){
        this.gameInfo = gameInfo
        this.room = room
        this.message='',
        this.time = 0 // 轮次
        this.secretNumber = ''
        this.guesses = 0
        this.timeLeft = 602
        this.timer = null,
          this.guessesValue = ''
        this.startGame = this.startGame
        this.checkGuess=this.checkGuess
        this.stopGame = this.stopGame
        this.sendRules = this.sendRules
        this.mounted()
    }

mounted(){
    console.log(this.room)
    setTimeout(() => {
        this.startGame()
    }, 1000);
}
    // 开始游戏时生成秘密数字并开始倒计时  
    startGame() {
        this.time++
        const firstStr = `大家一起猜数字，每题600秒，1A2B初始化完毕，我心中的想一个4位数字，你来猜，根据提示进行调整重新猜`
        let str = `第 ${this.time} 轮猜数字游戏准备开始 \n\n  请 @我 并发送4位数字，数字不能重复\n\n如果想要了解玩法，请说:"游戏规则"！ \n\n如果想停止游戏，请说“结束游戏”`
        if(this.time==1){
          this.room.say `${firstStr}\n\n${str}`
        }else{
          this.room.say(str)

        }
       
        this.generateSecretNumber();
        this.timer = setInterval( ()=> {
          this.timeLeft--;
            console.log(this.timeLeft)
          if (this.timeLeft <= 0) {
           this.room.say `时间到，正确答案是 \n \n  ${this.secretNumber} \n \n 很遗憾本轮没有人答对！`
            this.stopGame()

            setTimeout(() => {
                this.startGame()
            }, 2000);
          }
          if(this.timeLeft==300){
            this.room.say `猜数字游戏正在进行中，剩余时间 5分钟`
          }
          if(this.timeLeft==60){
            this.room.say `猜数字游戏正在进行中，剩余时间 1分钟`
          }
        }, 1000);
      }

          // 检查猜测并给出反馈  
    checkGuess(guessesValue,contact) {
        this.guessesValue =guessesValue || this.guessesValue
     
        let guess = this.guessesValue
        guess=guess.trim()
        this.guesses++;
        console.log(parseInt(guess))
        if (guess === this.secretNumber) {
           this.room.say `${contact} 恭喜你回答正确!答案为${ this.secretNumber}! 本轮游戏结束！！`
            this.stopGame()
            setTimeout(() => {
                this.startGame()
            }, 2000);
        } else if(guess.length==4 && parseInt(guess) ||parseInt(guess)==0 ) {
          let feedback = '';
          let Acout = 0
          let Bcout = 0
          let correctDigits = 0;
          for (let i = 0; i < guess.length; i++) {
            for (let j = i + 1; j <= guess.length; j++) {
               
              if (guess[i] == guess[j]) {
              this.room.say `${contact} 数字重复，请重新输入`
                return
                feedback = '';
                Acout = 0
                Bcout = 0
                correctDigits = 0;
              }
            }
            if (guess[i] === this.secretNumber[i]) {
              feedback += 'A';
              correctDigits++;
            } else if (this.secretNumber.indexOf(guess[i]) > -1) {
              feedback += 'B';
              Bcout++
            }
          }
         this.room.say ` ${contact} \n \n 回答错误\n \n 猜数:${guess}\n \n 提示:${correctDigits}A${Bcout}B;\n \n 剩余时间:${this.timeLeft}`
  
  
        }else{
            this.room.say `${contact} 大家正在玩猜数字游戏呢,有什么事玩完了再说吧！！`
            return
        }
        // 重置猜测框以供下次输入  
       
      }
      generateSecretNumber() {
        let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let result = '';
        while (numbers.length > 6) {
          let randomIndex = Math.floor(Math.random() * numbers.length);
          result += numbers[randomIndex];
          numbers.splice(randomIndex, 1);
        }
        this.secretNumber = result.trim();
        console.log(this.secretNumber)

      }
      stopGame(){
        clearInterval(this.timer);
        this.secretNumber = ''
        this.guesses = 0
        this.timeLeft = 602
        this.timer = null
      
      }
      sendRules(contact){
        this.room.say `${contact} \n\n ${this.gameInfo.rules}`
      }
}